import datetime
import copy
import numpy as np
import pandas as pd
import Core.Gadget as Gadget
from SystematicFactors.General import *


def Revenue_Q3(database, year):

    #
    filter = [("report_date", "20200930")]
    df_q3 = database.GetDataFrame("financial_data", "stock_fundamental", filter=filter, projection=["totalrevenue", "netincome2","symbol","report_date"])
    df_q3.rename(columns={"totalrevenue":"revenue_q3", "netincome2":"earning_q3"}, inplace=True)

    #
    filter = [("report_date", "20191231")]
    df_last = database.GetDataFrame("financial_data", "stock_fundamental", filter=filter,
                          projection=["totalrevenue", "netincome2", "symbol", "report_date"])
    df_last.rename(columns={"totalrevenue": "revenue_ly", "netincome2": "earning_ly"}, inplace=True)

    df = pd.merge(df_q3, df_last, how="left", on="symbol")
    df.to_csv("d:\\q3_vs_last_year.csv")
    #
    df1 = df[df["earning_q3"] > df["earning_ly"]]
    df1.to_csv("d:\\q3_vs_last_year_earning_selected.csv")
    #
    df2 = df[df["revenue_q3"] > df["revenue_ly"]]
    df2.to_csv("d:\\q3_vs_last_year_revenue_selected.csv")

    pass


if __name__ == '__main__':
    #
    from Core.Config import *
    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()

    datetime1 = datetime.datetime(2000, 1, 1)
    datetime2 = datetime.datetime(2020, 5, 11)

    Revenue_Q3(database, 2020)